/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbLogHead;

import storage.util.StorageManager;
/**
 * 
 * @author fenyo rome
 *
 */
public class LogHeadHelper {

	/**
	 * 将logHead保存到LaUDMS数据库中
	 * 
	 * @param logHead
	 *            要存储的模型对象
	 */
	public void saveLogHead(DbLogHead logHead) {
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_logHead);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logHead"));
		// sm.open();
		sm.setRowKey(logHead.getRowKey());
		sm.put(DbLogHead.CL_NAME, logHead.getName());
		sm.put(DbLogHead.CL_LOCATION, logHead.getLocation());
		sm.put(DbLogHead.CL_CHINESE, logHead.getChinese());
		// sm.close();

	}

	/**
	 * 将一系列logHead保存到LaUDMS数据库中
	 * 
	 * @param logHeads
	 *            要存储的模型对象列表
	 */
	public void saveLogHeads(List<DbLogHead> logHeads) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logHead"));
		// sm.open();
		for (int i = 0; i < logHeads.size(); i++) {
			DbLogHead logHead = logHeads.get(i);
			sm.setRowKey(logHead.getRowKey());
			sm.put(DbLogHead.CL_NAME, logHead.getName());
			sm.put(DbLogHead.CL_LOCATION, logHead.getLocation());
			sm.put(DbLogHead.CL_CHINESE, logHead.getChinese());
		}
		// sm.close();
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @return
	 */
	public List<DbLogHead> getLogHeads(final String columnName,
			final String value) {
		List<DbLogHead> result = new LinkedList<DbLogHead>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logHead"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, location = null, chinese = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbLogHead.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbLogHead.CL_LOCATION)) {
						location = columnsValue;
					} else if (columnsName.equals(DbLogHead.CL_CHINESE)) {
						chinese = columnsValue;
					}
				}
				DbLogHead pLogHead = new DbLogHead(rowKey, name, location,
						chinese);
				result.add(pLogHead);
			}
		} catch (Exception ex) {
			Logger.getLogger(DbLogHead.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return result;
	}
	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteLogHead(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logHead"));
		// sm.open();
		sm.delete(rowKey);
		// sm.close();
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllLogHeadName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logHead"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> logHeadNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbLogHead.CL_NAME)) {
						name = columnsValue;
					}
				}
				if (!logHeadNameList.contains(name)) {
					logHeadNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(LogHeadHelper.class.getName()).log(Level.SEVERE,
					null, ex);
		} 
		return logHeadNameList;
	}
	/**
	 * 
	 * @return Map<String, String>
	 */
	public static Map<String, String> getLogHeadsLocationMap() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logHead"));
		Map<String, String> headlocation = new HashMap<String, String>();
		List<KeySlice> crs = sm.getAll();
		String name = null, location = null;
		try {
			for (KeySlice ks : crs) {
				location = null;
				name = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbLogHead.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbLogHead.CL_LOCATION)) {
						location = columnsValue;
					}
				}
				headlocation.put(name, location);
			}
		} catch (Exception ex) {
			Logger.getLogger(DbLogHead.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return headlocation;
	}
}
